<template>
  <div class="app-container">
    <el-form :model="queryParams"
             ref="queryForm"
             size="small"
             :inline="true"
             v-show="showSearch"
             label-width="68px">
      <el-form-item label="物料名称"
                    prop="name">
        <el-input v-model="queryParams.name"
                  placeholder="请输入物料名称"
                  clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   icon="el-icon-search"
                   size="mini"
                   @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"
                   size="mini"
                   @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10"
            v-if="deptId===102"
            class="mb8">
      <el-col :span="1.5">
        <el-button type="primary"
                   plain
                   icon="el-icon-plus"
                   size="mini"
                   @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-if="deptId===102"
              :data="supplierMaterialList">
      <el-table-column label="物料编码"
                       align="center"
                       prop="code" />
      <el-table-column label="物料名称"
                       align="center"
                       prop="name" />
      <el-table-column label="生产入库"
                       align="center"
                       prop="production" />
      <el-table-column label="退料入库"
                       align="center"
                       prop="returnMaterial" />
      <el-table-column label="采购出库"
                       align="center"
                       prop="purchase" />
      <el-table-column label="现有库存"
                       align="center"
                       prop="existing" />
      <el-table-column label="操作"
                       align="center"
                       class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleSupplierUpdate(scope.row)">修改</el-button>
          <el-button size="mini"
                     type="text"
                     icon="el-icon-search"
                     @click="openSupplierEcharts(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-table v-if="deptId===101"
              :data="enterpriseMaterialList">
      <el-table-column label="物料名称"
                       align="center"
                       prop="name" />
      <el-table-column label="物料编码"
                       align="center"
                       prop="code" />
      <el-table-column label="初始库存"
                       align="center"
                       prop="initialNum" />
      <el-table-column label="采购入库"
                       align="center"
                       prop="purchaseNum" />
      <el-table-column label="退料出库"
                       align="center"
                       prop="returnNum" />
      <el-table-column label="正常出库"
                       align="center"
                       prop="normalNum" />
      <el-table-column label="现有库存"
                       align="center"
                       prop="existingNum" />
      <el-table-column label="操作"
                       align="center"
                       class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleEnterpriseUpdate(scope.row)">修改</el-button>
          <el-button size="mini"
                     type="text"
                     icon="el-icon-search"
                     @click="openEnterpriseEcharts(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getUserInfo" />

    <!-- 添加或修改供应商物料库存对话框 -->
    <el-dialog :title="supplierTitle"
               :visible.sync="supplierOpen"
               width="500px"
               append-to-body>
      <el-form ref="supplierForm"
               :model="supplierForm"
               :rules="supplierRules"
               label-width="80px">
        <el-form-item label="物料编码"
                      v-if="supplierForm.id==null"
                      prop="code">
          <el-input v-model="supplierForm.code"
                    placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称"
                      v-if="supplierForm.id==null"
                      prop="name">
          <el-input v-model="supplierForm.name"
                    placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="生产入库"
                      prop="production">
          <el-input v-model="supplierForm.production"
                    placeholder="请输入生产入库" />
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button type="primary"
                   @click="supplierSubmitForm">确 定</el-button>
        <el-button @click="supplierCancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="enterpriseTitle"
               :visible.sync="enterpriseOpen"
               width="500px"
               append-to-body>
      <el-form ref="enterpriseForm"
               :model="enterpriseForm"
               :rules="enterpriseRules"
               label-width="80px">
        <el-form-item label="初始库存"
                      prop="initialNum">
          <el-input v-model="enterpriseForm.initialNum"
                    placeholder="请输入初始库存" />
        </el-form-item>
        <el-form-item label="正常出库"
                      prop="normalNum">
          <el-input v-model="enterpriseForm.normalNum"
                    placeholder="请输入正常出库" />
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button type="primary"
                   @click="enterpriseSubmitForm">确 定</el-button>
        <el-button @click="enterpriseCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSupplierMaterial,
  getSupplierMaterial,
  addSupplierMaterial,
  updateSupplierMaterial,
  listEnterpriseMaterial,
  getEnterpriseMaterial,
  addEnterpriseMaterial,
  updateEnterpriseMaterial
} from "@/api/system/stock";
import { getUserProfile } from "@/api/system/user";

export default {
  name: "Stock",
  data () {
    return {
      deptId: null,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商物料库存表格数据
      supplierMaterialList: [],
      enterpriseMaterialList: [],
      // 弹出层标题
      supplierTitle: "",
      enterpriseTitle: "",
      // 是否显示弹出层
      supplierOpen: false,
      enterpriseOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      supplierForm: {},
      enterpriseForm: {},
      // 表单校验
      supplierRules: {
        code: [
          { required: true, message: '请输入物料编码', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请输入物料名称', trigger: 'blur' },
        ],
        production: [
          { required: true, message: '请输入正确的生产入库数量', trigger: 'blur' },
          { pattern: /^[1-9]\d*$/, message: "请输入正确的生产入库数量", trigger: "blur" }
        ]
      },
      enterpriseRules: {
        initialNum: [
          { required: true, message: '请输入正确的初始库存', trigger: 'blur' },
          { pattern: /^[1-9]\d*$/, message: "请输入正确的初始库存", trigger: "blur" }
        ],
        normalNum: [
          { required: true, message: '请输入正确的正常出库数量', trigger: 'blur' },
          { pattern: /^[1-9]\d*$/, message: "请输入正确的正常出库数量", trigger: "blur" }
        ]
      }
    };
  },
  created () {
    this.getUserInfo();
  },
  methods: {
    // 通用
    /** 获取我的用户信息 */
    getUserInfo () {
      getUserProfile().then((response) => {
        this.deptId = response.data.deptId;
        /** 查询物料库存列表 */
        if (this.deptId === 102) {
          // 供应商
          listSupplierMaterial(this.queryParams).then(response => {
            this.supplierMaterialList = response.rows;
            this.total = response.total;
          });
        } else if (this.deptId === 101) {
          // 企业
          listEnterpriseMaterial(this.queryParams).then(response => {
            this.enterpriseMaterialList = response.rows;
            this.total = response.total;
          });
        }
      });
    },
    // 跳转到详情页面，并且传递id
    openSupplierEcharts (row) {
      this.$router.push({ name: "SupplierStockDetial", query: { id: row.id } });
    },
    // 跳转到详情页面，并且传递id
    openEnterpriseEcharts (row) {
      this.$router.push({ name: "EnterpriseStockDetial", query: { id: row.id } });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getUserInfo();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 供应商专属
    // 供应商取消按钮
    supplierCancel () {
      this.supplierOpen = false;
      this.resetSupplier();
    },
    // 供应商表单重置
    resetSupplier () {
      this.supplierForm = {
        id: null,
        supplierId: null,
        code: null,
        name: null,
        production: null,
        returnMaterial: null,
        purchase: null,
        existing: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("supplierForm");
    },
    /** 供应商新增按钮操作 */
    handleAdd () {
      this.resetSupplier();
      this.supplierOpen = true;
      this.supplierTitle = "添加供应商物料库存";
    },
    /** 供应商修改按钮操作 */
    handleSupplierUpdate (row) {
      this.resetSupplier();
      const id = row.id
      getSupplierMaterial(id).then(response => {
        this.supplierForm = response.data;
        this.supplierOpen = true;
        this.supplierTitle = "修改供应商物料库存";
      });
    },
    /** 供应商提交按钮 */
    supplierSubmitForm () {
      this.$refs["supplierForm"].validate(valid => {
        if (valid) {
          if (this.supplierForm.id != null) {
            updateSupplierMaterial(this.supplierForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.supplierOpen = false;
              this.getUserInfo();
            });
          } else {
            addSupplierMaterial(this.supplierForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.supplierOpen = false;
              this.getUserInfo();
            });
          }
        }
      });
    },

    // 企业专属
    // 企业取消按钮
    enterpriseCancel () {
      this.open = false;
      this.enterpriseReset();
    },
    // 企业表单重置
    enterpriseReset () {
      this.enterpriseForm = {
        id: null,
        eid: null,
        code: null,
        name: null,
        purchaseNum: null,
        returnNum: null,
        normalNum: null,
        existingNum: null,
        initialNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("enterpriseForm");
    },
    /** 企业修改按钮操作 */
    handleEnterpriseUpdate (row) {
      this.enterpriseReset();
      const id = row.id
      getEnterpriseMaterial(id).then(response => {
        this.enterpriseForm = response.data;
        this.enterpriseOpen = true;
        this.enterpriseTitle = "修改企业物料库存";
      });
    },
    /** 企业提交按钮 */
    enterpriseSubmitForm () {
      this.$refs["enterpriseForm"].validate(valid => {
        if (valid) {
          if (this.enterpriseForm.id != null) {
            updateEnterpriseMaterial(this.enterpriseForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.enterpriseOpen = false;
              this.getUserInfo();
            });
          } else {
            addEnterpriseMaterial(this.enterpriseForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.enterpriseOpen = false;
              this.getUserInfo();
            });
          }
        }
      });
    },
  }
};
</script>
